<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace model;

use api\portal\model\MerchModel;
use app\admin\controller\NavMenuController;
use app\admin\model\NavMenuModel;
use think\Model;

class GoodsModel extends Model
{

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'goods';

    protected $type = [
        'more' => 'array',
    ];

    /**
     * 整理数据
     * @param $row
     * @return mixed
     */
    public static function decode(&$row)
    {
        $row['thumb'] = cmf_get_asset_url($row['thumb']);
        if (!empty($row['images'])) { 
            $row['images'] = json_decode($row['images'],true); 
           
            foreach ($row['images']  as &$item) {
                $item = cmf_get_asset_url($item);
            }
            unset($item);
        }
        //审核
        switch ($row['check']) {
            case 1 :
                $row['check'] = '通过';
                continue;
            case -1 :
                $row['check'] = '拒绝';
                continue;
            case 0 :
                $row['check'] = '待审核';
                continue;
        }
        //状态
        switch ($row['status']) {
            case 1 :
                $row['status'] = '上架';
                continue;
            case 0 :
                $row['status'] = '下架';
                continue;
        }

        $row['hot']  = $row['hot'] == 1 ? '热销' : '无';
        $row['sale']  = $row['sale'] == 1 ? '促销' : '无';
        $row['recomment']  = $row['recomment'] == 1 ? '推荐' : '无';

        $merch = MerchModel::where(['id' => $row['merch_id']])->field(['title'])->find();
        $row['merch'] = $merch['title'];
        $category = NavMenuModel::where(['id' => $row['category_id']])->field(['name', 'id'])->find();
        $row['category'] = $category['name'];
        if (isset( $row['comment'])){
            $row['comment'] = htmlspecialchars_decode($row['comment']);
        }

        return $row;
    }

}